/**   
* @{#} CproClimb.java Create on 2013-7-24 下午1:35:09   
*   
* Copyright (c) 2012 by Baidu.   
*/
package union.test;

import java.util.List;

import objtable.core.TableCenter;
import objtable.core.TableGenerator;
import objtable.port.CsvPort;
import objtable.port.DbPort;
import objtable.struct.DataType;
import objtable.struct.Row;
import union.func.DomainHangYeRate;
import union.func.DomainQualityRate;

/**   
* @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>  
* @version 1.0   
*/

public class NewCproClimb {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String date = "20130727";

		//3101
		DbPort db_main = new DbPort(
				"jdbc:mysql://yf-veyron-rd02.yf01.baidu.com:8600/union4",
				"uniondb", "uniondb123");

		//注册网盟爬坡函数
		DomainHangYeRate dhyr = new DomainHangYeRate(db_main);
		TableCenter.regComputeFunc("hangyerate", dhyr);

		//注册网盟质量星级函数
		DomainQualityRate dqr = new DomainQualityRate(db_main);
		TableCenter.regComputeFunc("qualityrate", dqr);

		//cpro分媒体收入业务表
		TableGenerator tg = new TableGenerator("cpromedium")
				.addField(DataType.INT, "customerid")
				.addField(DataType.INT, "tm")
				.addField(DataType.INT, "beidoutyperoot")
				.addField(DataType.INT, "beidoutypesub")
				.addField(DataType.DOUBLE, "viewtimes")
				.addField(DataType.DOUBLE, "income")
				.addField(DataType.DOUBLE, "divide")//真实收入
				.addField(DataType.DOUBLE, "rate");
		tg.regToCenter();
		db_main.fromDB(
				"cpromedium",
				"select a.customerid,a.tm,b.beidoutyperoot,b.beidoutypesub,sum(viewtimes) as viewtimes,sum(income) as income from daycpromediumsttt a,websiteinfo b where a.COUNTDATE="
						+ date
						+ " and a.CUSTOMERID = b.CUSTOMERID and a.TM = b.TM and b.state = 4 group by a.customerid,a.tm");

		//customer分媒体分成比例表
		tg = new TableGenerator("cusmediumdivide")
				.addField(DataType.INT, "customerid")
				.addField(DataType.INT, "tm")
				.addField(DataType.DOUBLE, "divide")
				.addField(DataType.INT, "tag")
				.addField(DataType.DOUBLE, "climbrate")
				.addField(DataType.DOUBLE, "qualityrate")
				.addField(DataType.DOUBLE, "tacrate")
				.addField(DataType.DOUBLE, "finalrate", 1.0d);
		tg.regToCenter();
		db_main.fromDB(
				"cusmediumdivide",
				"select a.customerid,a.tm,avg(divide) as divide,max(a.tag) as tag,ifnull(max(tac),1) as tacrate from cnmediumdivide a left join cnmediumtac b on a.cid=b.cid and a.tm=b.tm group by a.customerid,a.tm");

		//计算网盟爬坡比例
		TableCenter.computeTableData("cpromedium",
				"rate=hangyerate(beidoutyperoot,beidoutypesub,viewtimes);");

		//导入网盟爬坡比例
		TableCenter.matchTableData("cusmediumdivide", "cpromedium",
				"a.customerid==b.customerid && a.tm==b.tm",
				"a.climbrate=b.rate");

		//计算质量星级比例
		TableCenter.computeTableData("cusmediumdivide",
				"qualityrate=qualityrate(tm);");

		//计算最终比例
		TableCenter
				.computeTableData(
						"cusmediumdivide",
						"if(tag == 1){finalrate=divide;}else{finalrate=climbrate*qualityrate*tacrate;};");

		//导入最终比例
		TableCenter.matchTableData("cpromedium", "cusmediumdivide",
				"a.customerid==b.customerid && a.tm==b.tm",
				"a.rate=b.finalrate");

		//计算最终收入
		TableCenter.computeTableData("cpromedium",
				"divide=income*rate*0.65*0.95;");

		//sum所有数据
		List<Row> rows = TableCenter.getTableData("cpromedium");
		rows = TableCenter.orderTableData("cpromedium", "customerid,tm", true);
		CsvPort.toCsv(rows, "./test/cpromedium_j_new.csv",
				"customerid,tm,divide", null);
		double d = 0.0d;
		for (Row r : rows) {
			d += r.getDouble("divide");
		}
		System.out.println(d);
	}
}
